package org.csu.mypetstore.web.servlets;

import org.csu.mypetstore.domain.Account;
import org.csu.mypetstore.domain.Cart;
import org.csu.mypetstore.domain.Order;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

public class NewOrderFormServlet extends HttpServlet {
    private static final String NEW_ORDER = "/WEB-INF/jsp/order/NewOrderForm.jsp";
    private static final String SIGNONFORM = "/WEB-INF/jsp/account/SignonForm.jsp";
    private static final String ERROR = "/WEB-INF/jsp/common/Error.jsp";

    private Account account;
    private Cart cart;
    private Order order;
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request,response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        HttpSession session = request.getSession();

        account = (Account) session.getAttribute("account");
        cart = (Cart)session.getAttribute("cart");

        if (account == null) {
            session.setAttribute("loginMsg","Please Sign On");
            request.getRequestDispatcher(SIGNONFORM).forward(request,response);
        } else if (cart != null){
            order = new Order();
            order.initOrder(account,cart);

            session.setAttribute("order",order);
//            if(account != null){
//                HttpServletRequest httpRequest= request;
//                String strBackUrl = "http://" + request.getServerName() + ":" + request.getServerPort()
//                        + httpRequest.getContextPath() + httpRequest.getServletPath() + "?" + (httpRequest.getQueryString());
//
//                LogService logService = new LogService();
//                String logInfo = logService.logInfo(" ") + strBackUrl + " 跳转到新订单页面";
//                logService.insertLogInfo(account.getUsername(), logInfo);
//            }
            request.getRequestDispatcher(NEW_ORDER).forward(request,response);
        } else {
            session.setAttribute("message","No Cart");

//            Account account = (Account)session.getAttribute("account");
//
//            if(account != null){
//                HttpServletRequest httpRequest= request;
//                String strBackUrl = "http://" + request.getServerName() + ":" + request.getServerPort()
//                        + httpRequest.getContextPath() + httpRequest.getServletPath() + "?" + (httpRequest.getQueryString());
//
//                LogService logService = new LogService();
//                String logInfo = logService.logInfo(" ") + strBackUrl + " 生成订单页面信息错误";
//                logService.insertLogInfo(account.getUsername(), logInfo);
//            }
            request.getRequestDispatcher(ERROR).forward(request,response);
        }
    }
}
